package com.pxxy.yuyi.controller;

import com.github.pagehelper.PageInfo;
import com.pxxy.yuyi.entity.Admin;
import com.pxxy.yuyi.entity.News;
import com.pxxy.yuyi.entity.Teachers;
import com.pxxy.yuyi.service.NewsService;
import com.pxxy.yuyi.utils.ConstUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 *
 * @Author : XuWen
 * @create 2021/6/21 11:11
 */
@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    //前端展示新闻
    @RequestMapping("/show")
    public String show(Integer ntype, HttpServletRequest request,Integer pageNum){
        if (pageNum==null){
            pageNum=1;
        }
        PageInfo<News> pageInfo= newsService.selectNewsType(ntype,pageNum);
      request.setAttribute("pi",pageInfo);
        return "news";
    }
    //前端展示新闻详情
    @RequestMapping("/info")
    public String info(String nid,HttpServletRequest request){
        News news = newsService.selectByNid(nid);
        request.setAttribute("info",news);
        return "newsInfo";
    }
    //后台显示新闻信息
    @RequestMapping("/showNews")
    public String showNews(HttpServletRequest request,Integer pageNum,Integer ntype){
        //1.先要判断是否有登录
        //没有登录，则先进行登录
        if (pageNum==null){
            pageNum=1;
        }
        Admin admin = (Admin) request.getSession().getAttribute(ConstUtils.LOGIN_USER);
        //为空则先进行登录
        if (admin==null) {
            request.setAttribute("msg", "请先登录");
            return "admin/adminLogin";
        }else{
            PageInfo<News> info =  newsService.selectNewsType(ntype,pageNum);
            request.setAttribute("pi",info);
            return "admin/new";
        }
    }
    //后台删除新闻
    @RequestMapping("/deleteNews")
    public String deleteNews(Integer nid){
        Integer result = newsService.deleteById(nid);
        return "forward:/news/showNews";
    }
    //后台添加新闻
    @RequestMapping("/addNews")
    public String addNews(News news){
        Integer result= newsService.addNews(news);
        return "forward:/news/showNews";
    }

}
